import request from '@/utils/request'
import type { BasePageResponse } from '@/utils/request'
import type {BasePageParam, TimeRangeParam} from "@/api/types";

export interface Book {
  bid: number
  // 创建用户ID
  uId: number
  // 书籍名称
  name: string
  // 书籍标题
  title: string
  // 书籍简介
  intro: string
  // 书籍售价
  price: number
  // 书籍的图片
  imgs: string
  // 购买时的价格
  oldPrice: number
  // 购买时的图片（截图凭证什么的）
  oldImgs: string
  // 购买的时间
  shopTime: string
  // 学校编号
  sId: number
  // 书籍状态（0：正常，1：屏蔽，2：已卖出）
  status: number
  // 书籍展示封面
  cover: string
  // 浏览次数
  viewCount: number
  // 书籍发布时间
  createTime: string
  // 书籍最后一次更新时间
  updateTime: string
  typeList: BookType[]
  user: User
  explain: string
}
export interface User{
  uId: string
  sId: number
  openid: string
  unionid: string
  sessionKey: string
  name: string
  headUrl: string
  phone: number
  createTime: string
  updateTime: string

  school: School;
}
export interface BookType {
  id: number
  name: string
  createTime: string
  updateTime: string
}
export interface School {
  sid: number
  name: string
  type: number
  area: string
  createTime: string
  updateTime: string
}
export interface BookInsBody {
  bId?: number
  // 书籍名称
  name: string
  // 书籍标题
  title: string
  // 书籍简介
  intro: string
  // 书籍售价
  price: number | null
  // 书籍图片
  imgs: string
  // 购买时的加个
  oldPrice: number | null
  // 购买时的图片
  oldImgs: string
  // 购买时间
  shopTime: Date | null
  // 书籍类型
  bTypes: string
  // 书籍备注
  explain: string
}
export interface BookListParam extends BasePageParam{
  linkName: string
}
export const getBookList = (params: BookListParam) => {
  return request<BookListParam,null, BasePageResponse<Book>>('/book/list', 'GET',{ noToken: true, data: params})
}
export const insertBook = (formData: BookInsBody) => {
  return request('/book/insert', 'POST', { data: formData })
}
export const updateBook = (formData: BookInsBody) => {
  return request('/book/update', 'PUT', { data: formData })
}
export interface IssueHistoryParam extends BasePageParam, TimeRangeParam{
  linkName?: string
  uId: string
  status?: number
}
export const queryIssueHistory = (params: IssueHistoryParam) => {
  return request<IssueHistoryParam, null, BasePageResponse<Book>>('/book/queryIssueHistory', 'GET', { data: params })
}
export const deleteByIds = (bIds: number[]) => {
  return request<{ids: string}, null, null>('/book/deleteByIds', 'DELETE', { data: {ids: bIds.join(",")}})
}
export const getBookById = (bid: number) => {
  return request<null, {bid: number}, Book>('/book/getById/{bid}', 'GET', { repObj:{bid} })
}
export const putView = (bid: number) => {
  return request<null, {bid: number}, Book>('/book/view/{bid}', 'PUT', { repObj:{bid} })
}
export const putSetStatus = (bid: number, status: 0 | 1 | 2) => {
  return request<null, {bid: number, status: 0 | 1 | 2}, Book>('/book/setStatus/{bid}/{status}', 'PUT', { repObj:{bid, status} })
}